extends Card

var base_damage := 5


func get_default_tooltip() -> String:
	return tooltip_text % base_damage


func get_updated_tooltip(player_modifiers: ModifierHandler, enemy_modifiers: ModifierHandler) -> String:
	var modified_dmg := player_modifiers.get_modified_value(base_damage, Modifier.Type.DMG_DEALT)

	if enemy_modifiers:
		modified_dmg = enemy_modifiers.get_modified_value(modified_dmg, Modifier.Type.DMG_TAKEN)
		
	return tooltip_text % [modified_dmg, modified_dmg]


func apply_effects(targets: Array[Node], modifiers: ModifierHandler) -> void:
	# 立即造成伤害
	var damage_effect := DamageEffect.new()
	damage_effect.amount = modifiers.get_modified_value(base_damage, Modifier.Type.DMG_DEALT)
	damage_effect.sound = sound
	damage_effect.execute(targets)
	
	# 创建2回合后的延迟伤害效果
	create_delayed_damage_effect(targets, modifiers)

func create_delayed_damage_effect(targets: Array[Node], modifiers: ModifierHandler) -> void:
	# 检查是否有目标
	if targets.size() == 0:
		print("❌ 没有目标，无法创建延迟伤害效果")
		return
	
	# 通过第一个目标获取battle实例
	var battle = targets[0].get_tree().get_first_node_in_group("battle")
	if not battle or not battle.resource_scheduler:
		print("❌ 无法找到battle或resource_scheduler")
		return
	
	# 从目标数组中获取第一个目标，然后在场景中重新查找
	var first_target = targets[0]
	var target_name = first_target.name
	
	# 在场景中重新查找目标
	var scene_target = null
	if first_target is Enemy:
		# 如果是敌人，在enemy_handler中查找
		for enemy in battle.enemy_handler.get_children():
			if enemy.name == target_name and is_instance_valid(enemy):
				scene_target = enemy
				break
	elif first_target is Player:
		# 如果是玩家，直接使用battle.player
		if is_instance_valid(battle.player):
			scene_target = battle.player
	
	if not scene_target:
		print("❌ 无法在场景中重新找到目标: %s" % target_name)
		return
	
	# 创建延迟伤害效果
	var delayed_effect = ScheduledEffect.new()
	delayed_effect.id = "axe_delayed_damage_%s" % str(randi())  # 唯一ID
	delayed_effect.trigger_type = ScheduledEffect.TriggerType.TURN_COUNT
	delayed_effect.target_type = ScheduledEffect.TargetType.CUSTOM
	delayed_effect.trigger_turn = battle.current_turn + 2  # 2回合后触发
	delayed_effect.is_repeating = false
	delayed_effect.max_triggers = 1
	
	# 创建延迟伤害效果
	var delayed_damage = DelayedDamageEffect.new()
	delayed_damage.amount = modifiers.get_modified_value(base_damage, Modifier.Type.DMG_DEALT)
	delayed_damage.sound = sound
	
	# 使用append方法添加目标，并添加错误检查
	if is_instance_valid(scene_target):
		delayed_damage.targets.append(scene_target)
		print("✅ 成功添加目标到延迟效果: %s" % scene_target.name)
	else:
		print("❌ 目标无效，无法添加到延迟效果")
		return
	
	delayed_effect.custom_effects.append(delayed_damage)
	
	# 添加到调度器
	battle.resource_scheduler.add_scheduled_effect(delayed_effect)
	print("🗡️ Axe卡牌已添加延迟伤害效果，将在第%d回合触发，目标: %s" % [delayed_effect.trigger_turn, scene_target.name])
